#include "goal_think.h"

bool static is_composit() { return true; }

void add_subgoal(goal* subgoal, goal_evaluator* evaluator) {

    std::pair<goal*, goal_evaluator*> temp(subgoal, evaluator);

    subgoals.push(temp);

} //end add_subgoal

void terminate() {

    for(
    std::vector<std::pair<goal*, goal_evaluator*>>::iterator iter = subgoals.begin();
    iter != subgoals.end();
    ++iter) {

        delete iter->second;

        iter->first->terminate();
        delete iter->first;

    } //end for

    subgoals.clear();

} //end terminate

void activate() {

    double best = 0, temp;
    goal* best_goal;

    best = subgoals.front().second->evaluate();
    current_best_goal = subgoals.front().second();

    //linear time search for maximum value
    for(
        std::vector<std::pair<goal*, goal_evaluator*>>::iterator iter = subgoals.begin();
        iter != subgoals.end();
        ++iter) {

        temp = iter->second->evaluate();

        if(best < temp) {

            best = temp;
            current_best_goal = iter->first;

        } //end if
    } //end for
} //end activate

void process() {

    //activate the goal if it is not active
   if(!current_best_goal->is_active() ) {

       current_best_goal->activate();

   } //end if

    //cache the goal's return so we can report it
    current_best_goal->process();

    //if the best subgoal is anything other then active goal think should re-arbitrate a
    //new objective.
    if(!current_best_goal->is_active()) {

        activate();

    } //end if

} //end process
